package com.shouhuobao.bhi.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.tools.SharedPref;
import com.collectplus.express.view.VerticalViewPager;
import com.collectplus.express.wxapi.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * 首次登陆 引导页
 *
 * @author coffee <br>
 *         2016-1-9上午11:04:59
 */
public class GuildActivity extends BaseActivity2 implements View.OnClickListener {

    private VerticalViewPager mViewPager;

    private final int PAGE_SIZE = 5;// 引导页数量
    //
    private LinearLayout mStateLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        super.findViewById();
        setContentView(R.layout.guild);
        mViewPager = (VerticalViewPager) findViewById(R.id.viewpager);
        mStateLayout = (LinearLayout) findViewById(R.id.guild_dot_layout);
        //        mViewPager.setOrientation(DirectionalViewPager.VERTICAL);
        findViewById(R.id.guild_close).setOnClickListener(this);
        loadImage();
        selectPage(0);
        //        mViewPager.addOnPageChangeListener(mViewPager.new ViewPageChangeListener() {
        //            @Override
        //            public void onPageSelected(int position) {
        //                // super.onPageSelected(position);
        //                selectPage(position);
        //                // 最后一页
        //                if (position == mStateLayout.getChildCount() - 1) {
        //                    mStateLayout.setVisibility(View.GONE);
        //                    findViewById(R.id.guild_close).setVisibility(View.VISIBLE);
        //                } else {
        //                    mStateLayout.setVisibility(View.VISIBLE);
        //                    findViewById(R.id.guild_close).setVisibility(View.GONE);
        //                }
        //            }
        //        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectPage(position);
                // 最后一页
                if (position == mStateLayout.getChildCount() - 1) {
                    //                    mStateLayout.setVisibility(View.GONE);
                    findViewById(R.id.guild_close).setVisibility(View.VISIBLE);
                } else {
                    //                    mStateLayout.setVisibility(View.VISIBLE);
                    findViewById(R.id.guild_close).setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guild_close:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.out_right2left);
        SharedPref.setFirstOpen(Constants.isLogin, false);
    }

    private void loadImage() {
        // LinearLayout parms = new LinearLayout(this);
        List<View> pager = new ArrayList<View>();
        for (int i = 0; i < PAGE_SIZE; i++) {
            int resId = getResources().getIdentifier("guild_" + (i + 1), "drawable", getPackageName());
            LinearLayout parms = new LinearLayout(getContext());
            // ImageView image = new ImageView(getContext());
            parms.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            // parms.setScaleType(ScaleType.FIT_XY);
            // parms.setAdjustViewBounds(true);
            parms.setBackgroundResource(resId);
            pager.add(parms);
            // parms.addView(image);
        }
        mViewPager.setViewItems(pager);
    }

    /**
     * 设置选中的页
     *
     * @param position
     */
    private void selectPage(int position) {
        for (int i = 0; i < mStateLayout.getChildCount(); i++) {
            ImageView child = (ImageView) mStateLayout.getChildAt(i);
            if (position == i) {
                child.setImageResource(R.drawable.dot_light);
            } else {
                child.setImageResource(R.drawable.dot_dark);
            }
        }
    }
}
